﻿using Infrastructure;
using RazorEngine;
using RazorEngine.Configuration;
using RazorEngine.Templating;
using RazorEngine.Text;
using System.Collections.Concurrent;
using System.Reflection;
using static System.Net.Mime.MediaTypeNames;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace RazorGeneLib
{
    public class RazorGenerator<T> where T : class
    {
        private string _templetePath;

        public RazorGenerator()
        {
            Init();
        }

        private void Init()
        {
            try
            {
                // 初始化配置
                var config = new TemplateServiceConfiguration
                {
                    Debug = false,
                    EncodedStringFactory = new RawStringFactory(),
                    CachingProvider=new DefaultCachingProvider(),
                };

                // 创建引擎实例
                var service = RazorEngineService.Create(config);
                Engine.Razor = service;

                this._templetePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot\\CodeGenTemplate");

                if (!Directory.Exists(this._templetePath))
                {
                    return;
                }
                /*
                var templeteFiles = Directory.GetFiles(this._templetePath, "*.cshtml", SearchOption.AllDirectories);
                foreach (var file in templeteFiles)
                {
                    string template = File.ReadAllText(file);
                    FileInfo fileInfo = new FileInfo(file);
                    var templateKey = $"code_{fileInfo.Name}";
                    Engine.Razor.AddTemplate(templateKey, template);
                    try
                    {
                        Engine.Razor.Compile(templateKey, typeof(T));
                    }
                    catch (TemplateCompilationException complieEx)
                    {
                        var error = string.Join("\r\n", complieEx.CompilerErrors.Select(m => m.ErrorText));
                        //throw new Exception(error);
                    }
                    catch (Exception ex)
                    {

                    }
                }
                */
            }
            catch (Exception ex)
            {

            }
        }

        public string Generate(string templateKey, T model)
        {
            try
            {
                var templeteFiles = Directory.GetFiles(this._templetePath, "*.cshtml", SearchOption.AllDirectories);
                foreach (var file in templeteFiles)
                {

                    FileInfo fileInfo = new FileInfo(file);
                    var key = $"code_{fileInfo.Name}";
                    if (templateKey.Equals(key))
                    {
                        string template = File.ReadAllText(file);
                        string hashCode = FileUtil.HashFileName(template);
                        var result = Engine.Razor.RunCompile(template, hashCode, typeof(T), model);

                        return result;
                    }
                }
            }
            catch (TemplateCompilationException complieEx)
            {
                var error = string.Join("\r\n", complieEx.CompilerErrors.Select(m => m.ErrorText));
                throw new Exception(error);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return null;
        }
    }


}
